package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.minio.MinioProperties;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.web.admin.service.FileService;
import io.minio.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private MinioClient minioClient;
    @Autowired
    private MinioProperties minioProperties;

    @Override
    public String upload(MultipartFile file) {

      try{
          createBucket();
          String datePath = new SimpleDateFormat("/yyy/MM/dd/").format(new Date());
          String uuidName = UUID.randomUUID().toString()+"-"+file.getOriginalFilename();
          String path = datePath + uuidName;

          PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                  .bucket(minioProperties.getBucketName())
                  .object(path)
                  .contentType(file.getContentType())
                  .stream(file.getInputStream(),file.getSize(),-1)
                  .build();
  minioClient.putObject(putObjectArgs);
          String url = minioProperties.getEndpoint() +"/" + minioProperties.getBucketName() + path;
          return url;
      }catch (Exception e){
          e.printStackTrace();
         // throw new RuntimeException(e);
      throw new LeaseException(ResultCodeEnum.FAIL);
      }
    }



    public void createBucket() throws Exception{

        BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder()
                .bucket(minioProperties.getBucketName())
                .build();
        boolean flag = minioClient.bucketExists(bucketExistsArgs);


        if (!flag){
            MakeBucketArgs makeBucketArgs = MakeBucketArgs.builder()
                    .bucket(minioProperties.getBucketName())
                    .build();
            minioClient.makeBucket(makeBucketArgs);


            SetBucketPolicyArgs setBucketPolicyArgs =
                    SetBucketPolicyArgs.builder()
                            .bucket(minioProperties.getBucketName())
                            .config(getConfig())
                            .build();
            minioClient.setBucketPolicy(setBucketPolicyArgs);
        }

    }
    public  String getConfig(){
        return """
                             
                        {
                          "Statement" : [ {
                            "Action" : "s3:GetObject",
                            "Effect" : "Allow",
                            "Principal" : "*",
                            "Resource" : "arn:aws:s3:::%s/*"
                          } ],
                          "Version" : "2012-10-17"
                        }
                    
                """.formatted(minioProperties.getBucketName());
    }


}
